// pages/mes/my-home/my-home.js
const app = getApp();
import utils from '../../../utils/util.js';
Page({

    /**
     * 页面的初始数据
     */
    data: {
        userName: '',
        userInfo: {},
        isShow: true,
        CoverPhoto: 'https://hear2017.oss-cn-shanghai.aliyuncs.com/xianjian_wx_image/home1.png',
        WorksCover: 'https://hear2017.oss-cn-shanghai.aliyuncs.com/xianjian_wx_image/home2.png',
        contentNoneUrl: '../../../assets/image/content_null.png',
        parentType: [],
        currentIndex: 0,
        isVideo: false,
        serviceArr: null,
        isActive: false,
        evaluateObj:null,
        parentName:'',
        iconLive: '../../../assets/icon/icon_live_1.png'
    },
    lookClick() {
        this.setData({
            isActive: !this.data.isActive
        })
    },
    clickScore() {
        wx.navigateTo({
            url: '/pages/evaluate/evaluate?parmas=' + JSON.stringify(this.data.evaluateObj.OrderIdList) + "&userName=" + this.data.userInfo.Introduction.NickName
        })
    },
    editEvent() {
        // let content = this.data.userInfo.Introduction.Introduction;
        // let label = this.data.userInfo.Introduction.Label;
        // let worksCover = this.data.WorksCover;
        let coverPhoto = this.data.userInfo.Introduction.CoverPhoto;
        if(!coverPhoto){
            coverPhoto = this.data.CoverPhoto;
        }
        let date = {
            content:this.data.userInfo.Introduction.Introduction,
            label: this.data.userInfo.Introduction.Label,
            coverPhoto: coverPhoto
        }
        wx.navigateTo({
            url: '/pages/mes/edit-home/edit-home?parmas=' + JSON.stringify(date).replace("=" , "")
        })
    },
    selectServiceItem(e) {
        let index = e.currentTarget.dataset.index;
        let dataObj = this.data.serviceArr[index];
        app.globalData.serviceContent = dataObj.ServiceContent;
        let parmas = JSON.stringify(dataObj).replace(/&nbsp;/g, ' ');
        wx.navigateTo({
            url: '/pages/mes/issue-detail/issue-detail?parmas=' + parmas
        })
    },
    clickMore() {
        let obj = {
            userInfoId: this.data.userInfo.Introduction.UserInfoId,
            falg: true
        }
        wx.navigateTo({
            url: '/pages/mes/more-product/more-product?parmas=' + JSON.stringify(obj),
        })
    },
    openLive() {
        app.globalData.globalWebUrl = this.data.userInfo.LiveInfo.LiveUrl;
        wx.navigateTo({
            url: '/pages/live-web/live-web?title=直播',
        })
    },
    clickWall() {
        let fileUrl = this.data.WorksCover;
        if (this.data.isVideo) {
            app.globalData.globalWebUrl = fileUrl;
            wx.navigateTo({
                url: '/pages/agreement-web/agreement-web?title=视频播放'
            })
        } else {
            wx.previewImage({
                current: fileUrl,
                urls: [fileUrl]
            })
        }
    },
    selectAllTypes() {
        wx.navigateTo({
            url: '/pages/mes/issue/issue'
        })
    },
    startLiveAnimation() {
        let index = 1;
        let that = this;
        let timer = setInterval(function () {
            if (++index > 3) {
                index = 1;
            }
            let urlTemp = `../../../assets/icon/icon_live_${index}.png`;
            that.setData({
                iconLive: urlTemp
            });
        }, 400);
        if (that.data.timer == null) {
            that.setData({
                timer: timer
            });
        }
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.setData({
            userName: options.userName
        });
        this.getParentTypes();
    },
    selectTypes(e) {
        let index = e.target.dataset.index;
        if (this.data.currentIndex == index) {
            return;
        }
        this.setData({
            currentIndex: index
        });
        wx.showLoading({
            title: '加载中...',
            mask: true
        });
        this.getUserEvaluate(app.userInfo.Id, this.data.parentTypes[index].Id);
        this.getUserService(this.data.parentTypes[index].Id);
    },
    getUserEvaluate(userId, parentId) {
        console.log(userId+","+parentId);
        let that = this;
        wx.showLoading({
            title: '加载中...',
            mask: true,
        })
        wx.request({
            url: app.globalData.serverAddress.getUserEvaluate,
            data: {
                userinfoId: userId,
                parentServiceType: parentId
            },
            header: {
                'Authorization': "Bearer " + app.globalData.token
            },
            success: function (res) {
                console.log(res);
                if (res.statusCode == 200 && res.data.CodeStatus == 20000) {
                    that.setData({
                        evaluateObj: res.data.BodyContent
                    });
                } else {
                    console.log("success：" + JSON.stringify(res));
                }
            },
            fail: function (res) {
                console.log("fail" + JSON.stringify(res));
            },
            complete: function (res) { wx.hideLoading(); }
        })
    },
    getParentTypes() {
        let that = this;
        wx.request({
            url: app.globalData.serverAddress.getMyPraentTypes,
            data: {
                userInfoId: app.userInfo.Id,
                currentUserInfoId: app.userInfo.Id
            },
            header: {
                'Authorization': "Bearer " + app.globalData.token
            },
            method: 'GET',
            success: function(res) {
                if (res.statusCode == 200 && res.data.CodeStatus == 20000) {
                    // console.log(JSON.stringify(res.data.BodyContent))
                    let list = res.data.BodyContent;
                    if (list != null && list.length > 0) {
                        that.setData({
                            parentTypes: list,
                            parentName: list[that.data.currentIndex].TypeName
                        });
                        that.getUserService(res.data.BodyContent[0].Id);
                        that.getUserEvaluate(app.userInfo.Id, res.data.BodyContent[0].Id);
                    }
                } else {
                    console.log("success：" + JSON.stringify(res));
                }
            },
            fail: function(res) {
                console.log("fail:" + JSON.stringify(res));
            }
        })
    },
    getUserService(parentId) {
        let that = this;
        wx.request({
            url: app.globalData.serverAddress.getUserService,
            data: {
                pageSize: 2,
                pageIndex: 0,
                userinfoId: app.userInfo.Id,
                currentUserInfoId: app.userInfo.Id,
                parentServiceType: parentId
            },
            header: {
                'Authorization': "Bearer " + app.globalData.token
            },
            success: function(res) {
                if (res.statusCode == 200 && res.data.CodeStatus == 20000) {
                    let newList = res.data.BodyContent.List;
                    // console.log(JSON.stringify(newList))
                    newList.forEach(function(item, index) {
                        newList[index]._amount = utils.formatAmount(item.Price);
                    });
                    that.setData({
                        serviceArr: newList
                    });
                } else {
                    console.log("success：" + JSON.stringify(res));
                }
            },
            fail: function(res) {
                console.log("fail" + JSON.stringify(res));
            },
            complete: function(res) {
                wx.hideLoading();
            }
        })
    },
    getAllInfo() {
        let that = this;
        wx.request({
            url: app.globalData.serverAddress.getAllInfo,
            data: {
                userId: app.userInfo.Id
            },
            header: {
                'Authorization': "Bearer " + app.globalData.token
            },
            method: 'GET',
            success: function(res) {
                if (res.statusCode == 200 && res.data.CodeStatus == 20000) {
                    console.log("getAllInfo" , res.data);
                    let BodyContent = res.data.BodyContent;
                    console.log(BodyContent)
                    let isShow = true;
                    let CoverPhoto = that.data.CoverPhoto;
                    let WorksCover = that.data.WorksCover;
                    let _CoverPhoto = BodyContent.Introduction.CoverPhoto;
                    let _WorksCover = BodyContent.Introduction.WorksCover;
                    let isVideo = false;
                    if (BodyContent.LiveInfo) {
                        that.startLiveAnimation();
                    }
                    if (BodyContent.Introduction.Introduction) {
                        isShow = false;
                    } else {
                        isShow = true;
                    }
                    if (_CoverPhoto) {
                        CoverPhoto = _CoverPhoto;
                    }
                    if (_WorksCover) {
                        if (_WorksCover.substring(_WorksCover.lastIndexOf('.') + 1) == 'mp4') {
                            isVideo = true;
                        }
                        WorksCover = _WorksCover;
                    }
                    that.setData({
                        userInfo: BodyContent,
                        isShow: isShow,
                        CoverPhoto: CoverPhoto,
                        WorksCover: WorksCover,
                        isVideo: isVideo
                    });
                } else {
                    console.log("success：" + JSON.stringify(res));
                }
            },
            fail: function(res) {
                console.log("fail" + JSON.stringify(res));
            }
        });
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        this.getAllInfo();
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {
        if (this.data.timer != null) {
            clearInterval(this.data.timer);
            this.setData({
                timer: null
            });
        }
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})